
//
//  RoomChatCell.m
//  MooreDoll
//
//  Created by Joe on 2014/12/10.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "RoomChatCell.h"
#import <ExternalAccessory/ExternalAccessory.h>

@implementation RoomChatCell

@synthesize IV_UserPic,LB_message,LB_name,LB_update,BT_music,isCellPlay,myplayer,chatMessage,sid;
//@synthesize chat;


//static int audioPlayCount = 0;

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)cellSettingWithChatMessage:(ChatMessage *)message andImageArray:(NSMutableArray *)images
{
    
    //set 使用者圖像
    NSString *ownerJID = message.fromWho;
    for (NSDictionary *memberDic in images)
    {
        NSString *memberJID = [self jidAdjustWithJID:[memberDic objectForKey:@"jid"]];
        
        if ([ownerJID isEqualToString:memberJID])
        {
            id image = [memberDic objectForKey:@"image"];
            
            if ([images isKindOfClass:[UIImage class]])
                [IV_UserPic setImage:image];
            else
                [IV_UserPic setImage:[UIImage imageNamed:@"dficon_user"]];
            
            [LB_name setText:[memberDic objectForKey:@"nickName"]];
            break;
        }
    }
    
    NSDateFormatter *dateFormate = [[NSDateFormatter alloc] init] ;
    [dateFormate setDateFormat:@"HH:mm"];
    NSString          *dayString = [dateFormate stringFromDate:message.update];
    LB_update.text               = dayString;
    
    if (![message.message isEqualToString:@""])
    {
        // message
        LB_message.text = message.message;
        LB_message.hidden = NO;
        BT_music.hidden = YES;
    }
    else
    {
        // 音樂
        LB_message.hidden = YES;
        BT_music.hidden = NO;
    }
    
    chatMessage = message;
    sid         = message.sid;

}

-(void)progressValueChange:(NSDictionary *)progressDic
{
    
    NSDictionary        *dic       = progressDic;
    float        progressValue     = [[dic objectForKey:@"ftp_send_progress"] floatValue]/(float)100;
    PV_Progress.progress           = progressValue;

    
    if (PV_Progress.progress >= 1)
    {
        PV_Progress.hidden = YES;
        BT_music.hidden    = NO;
        LB_message.hidden  = YES;
    }
    else
    {
        PV_Progress.hidden = NO;
        LB_message.hidden  = YES;
        BT_music.hidden    = YES;
    }
}

-(IBAction)musicButtonPress:(UIButton *)sender
{
    [self.delegate cellsMusicPlayWithCell:self];
}


-(void)playMusic
{
    
    if (!myplayer)
    {
        NSString *path = [NSHomeDirectory() stringByAppendingString:chatMessage.audioPath];
        if ([[NSFileManager defaultManager] fileExistsAtPath:path])
        {
            NSURL *url = [NSURL fileURLWithPath:path];
            NSError *error = nil;
            myplayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
            [myplayer prepareToPlay];
        }
    }
    
    if (!myplayer.isPlaying)
    {
        NSLog(@"%@",chatMessage.audioPath);
        [myplayer play];
        
    }
    else
    {
        [myplayer stop];
        
    }
}
-(void)stopMusic
{
    if (myplayer)
        [myplayer stop];
    
}


-(NSString *)jidAdjustWithJID:(NSString *)jid
{
    NSString *adjustJID = jid;
    
    if ([adjustJID rangeOfString:@"/"].location != NSNotFound)
        adjustJID = [adjustJID substringToIndex:[adjustJID rangeOfString:@"/"].location];
    
    if ([adjustJID rangeOfString:@"@bbserver-virtualbox"].location == NSNotFound)
        adjustJID = [adjustJID stringByAppendingString:@"@bbserver-virtualbox"];
    
    return adjustJID;
}
@end

